import datetime
import time
#from datetime import datetime

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.expected_conditions import invisibility_of_element_located

from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.microsoft import EdgeChromiumDriverManager
#创建驱动对象
#1.打开浏览器---驱动管理
##安装对应浏览器驱动，并保存路径
# # EdgeIns=EdgeChromiumDriverManager().install()
# driver_path = 'D:\\edgedriver\\msedgedriver.exe'
# ###创建Edge浏览器驱动对象
# driver=webdriver.Edge(service=Service(driver_path))
# time.sleep(2)
#2.输入百度网址：https://www.baidu.com（网址一定要完整，不要写成www.baidu.com）
# driver.get("https://www.baidu.com")
#time.sleep(2)
#3.找到百度输入框，输入关键词“迪丽热巴”
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# # time.sleep(2)
# #4.点击“百度一下”按钮
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)

# driver.find_element(By.CSS_SELECTOR,"#kw").click()
# time.sleep(2)
#
# ele=driver.find_element(By.CSS_SELECTOR,"#kw")
# ele.send_keys("萌宠")

# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("萌宠乐园")
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,"#kw").clear()
# time.sleep(2)
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("娱乐明星")

# #获取文本信息
# text=driver.find_element(By.CSS_SELECTOR,"#hotsearch-content-wrapper > li:nth-child(7) > a").text
# print(text)
# #获取到元素对应的文本信息后，通过断言来判断文本信息是否符合预期
# assert text=="3“村超”持续助推乡村振兴"
# value=driver.find_element(By.CSS_SELECTOR,"#su").get_attribute("value")
# print(value)
#
# #适用场景：页面元素点击可跳转的情况下，用来检测跳转的结果是否是正确的
# #获取页面的标题
# title=driver.title
# #获取当前页面的url
# url=driver.current_url
#
# print(title)
# print(url)


#time.sleep(3)

#点击百度图片功能，跳转到百度图片页面

# print("before title:"+driver.title)
# print("before url:"+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > div > a").click()
# driver.find_element(By.CSS_SELECTOR,"#content > div:nth-child(25) > div:nth-child(2) > a")
# time.sleep(2)
# print("after title:"+driver.title)
# print("after url:"+driver.current_url)

# print("before:"+driver.title)
# print("before:"+driver.current_url)
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > div > a").click()
# #获取当前页面的句柄---第一个标签页
# curHandle=driver.current_window_handle
# #获取所有句柄
# allHandle=driver.window_handles
# #遍历所有句柄，切换到新的页面
# for handle in allHandle:
#     if handle != curHandle:
#         #切换句柄
#         driver.switch_to.window(handle)
# #测试跳转结果
# print("after:"+driver.title)
# print("after:"+driver.current_url)

# #屏幕截图
# filename='./test.png'
# filename = "./images/test1.png"
# driver.save_screenshot(filename)

# #进阶版本：每次生成的图片文件都不一样
# #百度首页
# #autotest-2025-09-14-1911
# # print(datetime.datetime.now())
# filename="autotest-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
# driver.save_screenshot("./images/"+filename)

# # 关闭窗口
# # driver.close():退出一个窗口
# # driver.quit():退出浏览器 当执行driver.qiut()后，所有的标签页都会被关掉
# time.sleep(2)
# # 打开新的标签页：百度更多
# driver.find_element(By.CSS_SELECTOR,"#s-top-left > div > a").click()
# time.sleep(3)
# # 关闭的是driver对象指向的标签页
# # driver.close()
# # 切换当前句柄为最新页面
# allHandle = driver.window_handles
# curHandle = driver.current_window_handle
# for handle in allHandle:
#     if handle != curHandle:
#         driver.switch_to.window(handle)
# driver.close()
# time.sleep(3)

# # 隐式等待
# driver.implicitly_wait(3)

# driver.get("https://ww.baidu.com")
# # driver.find_element(By.CSS_SELECTOR,"#chat-textarea").send_keys("迪丽热巴")
# # driver.find_element(By.CSS_SELECTOR,"#chat-submit-button").click()
# # driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("王一博")
# # time.sleep(1)
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# # time.sleep(1)
# driver.find_element(By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(2) > span")
# # time.sleep(1)

# # 显示等待
#
# driver.get("https://ww.baidu.com")
# # driver.find_element(By.CSS_SELECTOR,"#chat-textarea").send_keys("迪丽热巴")
# # driver.find_element(By.CSS_SELECTOR,"#chat-submit-button").click()
# # driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("王一博")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# # 添加显示等待
# # 创建显示等待类对象
# wait = WebDriverWait(driver,3)
# wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(2) > span")))
#
# # driver.find_element(By.CSS_SELECTOR,"#\\31  > div > div > div > div > div > div.new-tag_4ozgi.new-text-link_3k9GD > div > div.flex-wrapper-top_3ucFS > div.flex-col-left_3trtY.baike-wrapper_6AORN.cu-pt-xs-lg.baike-wrapper-pc_26R04.cu-pt-xl.baike-wrapper-left-pc_5eYY8.cos-space-pb-sm > div > div > p > span:nth-child(2) > span")

# # 前进后退刷新
# driver.get("https://www.baidu.com")
# driver.find_element(By.CSS_SELECTOR,"#kw").send_keys("迪丽热巴")
# driver.find_element(By.CSS_SELECTOR,"#su").click()
# time.sleep(2)
# driver.back()
# time.sleep(2)
# driver.forward()
# time.sleep(2)
# driver.refresh()
# time.sleep(2)


# 打开浏览器----驱动管理
driver_path='D:\\edgedriver\\msedgedriver.exe'
# 浏览器参数配置
options=webdriver.EdgeOptions()
# # 添加无头模式
# options.add_argument("-headless")

# 添加页面加载策略
# options.page_load_strategy='normal'  #等待所有的资源加载完成
# options.page_load_strategy='eager'   #DOM访问就绪
options.page_load_strategy='none'      #完全不阻塞，直接继续往下执行脚本
# 创建谷歌浏览器驱动对象
driver=webdriver.Edge(service=Service(driver_path),options=options)
driver.get("https://haokan.baidu.com")
print(driver.title)


#5.关闭浏览器
driver.quit()
